#!usr/bin/env python
#-*- coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 最新资料 ->右栏：公司备忘
爬取类目：公司备忘
股票类型不包括港股（港股没有最新资料）
url_sample: http://www.cninfo.com.cn/information/memo/szmb000001.html
"""
"""
result_sample_of_company_memo = {
    '_id': '',
    'stockCode': '',
    'companyShortName': '',
    'stockType': '',
    'memo':[{
        'memoEvent',
        'memoDate',
    },],
}
"""

import sys

from company_formaltable_crawler import CompanyFormalTable
from company_crawler import CompanyInfoUtil
from xtls.logger import get_logger
from xtls.timeparser import now

reload(sys)
sys.setdefaultencoding('utf-8')

CATEGORY = 'memo'

DETAIL_MAPPING = {
    u'备忘事件': 'memoEvent',
    u'备忘时间': 'memoDate',
}

class CompanyMemo(CompanyFormalTable):
    def __init__(self, company_info_util_instance):
        super(CompanyMemo, self).__init__(company_info_util_instance=company_info_util_instance)

    def save(self, data):
        data['updateTime'] = now()
        for key, value in data.items():
            if key in ['memoDetail']:
                for memo_list in value:
                    for child_key, child_value in memo_list.items():
                        print '{}: {}'.format(child_key, child_value)
                print '-'*40
            else:
                print '{}: {}'.format(key, value)

        print "=" * 70

    def run(self, startType=4, endType=4):
        super(CompanyFormalTable, self).run(startType=startType, endType=endType)

def main():
    global CATEGORY
    global DETAIL_MAPPING
    from_company_info = CompanyInfoUtil(category=CATEGORY,
                                        detail_mapping=DETAIL_MAPPING)
    from_company_info.set_logger(get_logger(__file__))
    company_memo = CompanyMemo(from_company_info)
    company_memo.set_select_path('div.zx_list')

    # try:
    company_memo.run()
    # except:
    #     from_company_info.logger.warn('%s shuttdown' % from_company_info.category)


if __name__ == '__main__':
    main()
